
library(list)
library(openxlsx)
library(kableExtra)
library(dplyr)
library(knitr)
library(rmarkdown)

soc.all <- read.xlsx("Data/data_list_experiment_for_R_final.xlsx")
soc.all$dum_1 <- ifelse((soc.all$group_1==1), 1, 0)
soc.all$dum_2 <- ifelse((soc.all$group_1==2), 1, 0)
soc.all$dum_3 <- ifelse((soc.all$group_1==3), 1, 0)
soc.all$dum_4 <- ifelse((soc.all$group_1==4), 1, 0)
soc.all$panel <- ifelse((soc.all$Referer==3), 1, 0)

## 1. Test for No Design Effect (i.e. that inclusion of a treatment item does not change the answers to control items)
# Participation (Table C3)
soc1 <- subset(soc.all, dum_2==0 & dum_3==0 & is.na(v16_1)==0)
res1 <- ict.test(soc1$v16_1, soc1$dum_1, J=4, alpha=0.05, n.draws=250000, gms=TRUE, pi.table=TRUE)
yesq1 <- round((matrix(unname(res1$pi.table[1:5, "est."])))*100,2)
noq1 <- round((matrix(unname(res1$pi.table[6:10, "est."])))*100,2)
pq1 <- round((matrix(unname(res1$p[[1]]))),3)
yesq1 <- matrix(c(yesq1, pq1))
noq1 <- matrix(c(noq1, NA))
print(yesq1)
print(noq1)

soc2 <- subset(soc.all, dum_1==0 & dum_3==0 & is.na(v16_1)==0)
res2 <- ict.test(soc2$v16_1, soc2$dum_2, J=4, alpha=0.05, n.draws=250000, gms=TRUE, pi.table=TRUE)
yesq2 <- round((matrix(unname(res2$pi.table[1:5, "est."])))*100,2)
noq2 <- round((matrix(unname(res2$pi.table[6:10, "est."])))*100,2)
pq2 <- round((matrix(unname(res2$p[[1]]))),3)
yesq2 <- matrix(c(yesq2,pq2))
noq2 <- matrix(c(noq2, NA))
print(yesq2)
print(noq2)

soc3 <- subset(soc.all, dum_1==0 & dum_2==0 & is.na(v16_1)==0)
res3 <- ict.test(soc3$v16_1, soc3$dum_3, J=4, alpha=0.05, n.draws=250000, gms=TRUE, pi.table=TRUE)
yesq3 <- round((matrix(unname(res3$pi.table[1:5, "est."])))*100,2)
noq3 <- round((matrix(unname(res3$pi.table[6:10, "est."])))*100,2)
pq3 <- round((matrix(unname(res3$p[[1]]))),3)
yesq3 <- matrix(c(yesq3,pq3))
noq3 <- matrix(c(noq3, NA))
print(yesq3)
print(noq3)

# Non-participation
soc4 <- subset(soc.all, dum_2==0 & dum_3==0 & is.na(v18_1)==0)
res4 <- ict.test(soc4$v18_1, soc4$dum_1, J=4, alpha=0.05, n.draws=250000, gms=TRUE, pi.table=TRUE)
yesq4 <- round((matrix(unname(res4$pi.table[1:5, "est."])))*100,2)
noq4 <- round((matrix(unname(res4$pi.table[6:10, "est."])))*100,2)
pq4 <- round((matrix(unname(res4$p[[1]]))),3)
yesq4 <- matrix(c(yesq4,pq4))
noq4 <- matrix(c(noq4, NA))
print(yesq4)
print(noq4)

soc5 <- subset(soc.all, dum_1==0 & dum_3==0 & is.na(v18_1)==0)
res5 <- ict.test(soc5$v18_1, soc5$dum_2, J=4, alpha=0.05, n.draws=250000, gms=TRUE, pi.table=TRUE)
yesq5 <- round((matrix(unname(res5$pi.table[1:5, "est."])))*100,2)
noq5 <- round((matrix(unname(res5$pi.table[6:10, "est."])))*100,2)
pq5 <- round((matrix(unname(res5$p[[1]]))),3)
yesq5 <- matrix(c(yesq5,pq5))
noq5 <- matrix(c(noq5, NA))
print(yesq5)
print(noq5)

soc6 <- subset(soc.all, dum_1==0 & dum_2==0 & is.na(v18_1)==0)
res6 <- ict.test(soc6$v18_1, soc6$dum_3, J=4, alpha=0.05, n.draws=250000, gms=TRUE, pi.table=TRUE)
yesq6 <- round((matrix(unname(res6$pi.table[1:5, "est."])))*100,2)
noq6 <- round((matrix(unname(res6$pi.table[6:10, "est."])))*100,2)
pq6 <- round((matrix(unname(res6$p[[1]]))),3)
yesq6 <- matrix(c(yesq6,pq6))
noq6 <- matrix(c(noq6, NA))
print(yesq6)
print(noq6)

#Build table
# Create the data as a matrix
table_data <- cbind(yesq1, noq1, yesq2, noq2, yesq3, noq3, yesq4, noq4, yesq5, noq5, yesq6, noq6)

# Convert to dataframe
df <- data.frame(
  "Number of control items agreed to" = c("0 motives", "1 motive", "2 motives", "3 motives", "4 motives", "Blair and Imai (2012) test p-value"),
  "Q1 Yes" = paste0(table_data[,1], "%"),
  "Q1 No" = paste0(table_data[,2], "%"),
  "Q2 Yes" = paste0(table_data[,3], "%"),
  "Q2 No" = paste0(table_data[,4], "%"),
  "Q3 Yes" = paste0(table_data[,5], "%"),
  "Q3 No" = paste0(table_data[,6], "%")
)
df[nrow(df), 2:ncol(df)] <- gsub("%", "", df[nrow(df), 2:ncol(df)])
print(df)
